#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

#include <algorithm>
#include <stack>

using namespace std;

int N;
int Input[5];

struct Node {
  int val;
  bool isnum;
};

Node Nodes[20];
bool Used[20];

const int Invalid = -999;
int Ans = Invalid;

int Compute() {
  // 计算后缀表达式。
  stack<int> S;
  for (int i = 0; i < 7; ++i) {
    Node& n = Nodes[i];
    // printf("# val %d isnum %d\n", n.val, n.isnum);

    if (n.isnum) {
      S.push(n.val);
      continue;
    }

    if (S.empty()) return Invalid;
    int b = S.top();
    S.pop();
    if (S.empty()) return Invalid;
    int a = S.top();
    S.pop();

    int ans = Invalid;
    switch (n.val) {
      case 0:
        ans = a + b;
        break;
      case 1:
        ans = a - b;
        break;
      case 2:
        ans = a * b;
        break;
      case 3:
        // 小心除零错误。
        if (b && a % b == 0) {
          ans = a / b;
        }
        break;
      default:
        break;
    }

    if (ans == Invalid) {
      return ans;
    }
    S.push(ans);
  }

  if (S.size() != 1 || S.top() > 24) {
    return Invalid;
  }
  assert(S.size() == 1);
  return S.top();
}

void PrintNodes() {
  for (int i = 0; i < 7; ++i) {
    Node& n = Nodes[i];
    printf("val %d isnum %d\n", n.val, n.isnum);
  }
  puts("");
}

void Solve2(int cur) {
  if (cur == 7) {
    // PrintNodes();
    int ans = Compute();
    // printf("ans %d\n", ans);
    if (ans == Invalid) {
      return;
    }
    if (Ans == Invalid || ans > Ans) {
      Ans = ans;
    }
    return;
  }

  if (Nodes[cur].isnum) {
    for (int i = 0; i < 4; ++i) {
      if (!Used[i]) {
        Used[i] = 1;
        Nodes[cur].val = Input[i];
        Solve2(cur + 1);
        Used[i] = 0;
      }
    }
  } else {
    for (int i = 0; i < 4; ++i) {
      Nodes[cur].val = i;
      Solve2(cur + 1);
    }
  }
}

void Solve(int cur) {
  if (cur == 7) {
    if (!Nodes[0].isnum || !Nodes[1].isnum) {
      return;
    }
    int ops = 0, nums = 0;
    for (int i = 0; i < 7; ++i) {
      if (Nodes[i].isnum) {
        nums++;
      } else {
        ops++;
      }
    }
    if (!(ops == 3 || nums == 4)) {
      return;
    }
    memset(Used, 0, sizeof(Used));
    Solve2(0);
    return;
  }

  Nodes[cur].isnum = false;
  Solve(cur + 1);
  Nodes[cur].isnum = true;
  Solve(cur + 1);
}

int main() {
//   freopen("in.txt", "r", stdin);

  scanf("%d", &N);

  while (N--) {
    for (int i = 0; i < 4; ++i) {
      scanf("%d", &Input[i]);
    }

    Ans = Invalid;
    memset(Used, 0, sizeof(Used));
    Solve(0);

    printf("%d\n", Ans);
  }

  return 0;
}